//
//  LHDBManager.m
//  EPA
//
//  Created by 卢浩 on 16/4/26.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHDBManager.h"
#import "LHReadListModel.h"
#import <FMDB.h>
#import "LHUserInfoManager.h"
#import "LHReadCollectModel.h"

@implementation LHDBManager

//单例
+ (instancetype)shareManager{
    static LHDBManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[LHDBManager alloc]init];
    });
    return manager;
}

#pragma mark ---- 打开,关闭数据库

//数据库指针
static FMDatabase *database = nil;

//打开个人数据库
- (void)openDB{
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    //数据库路径
    NSString *dbPath = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.sqlite",[LHUserInfoManager shareManager].uid]];
    NSLog(@"%@",dbPath);
    
    //打开,创建数据库
    database = [FMDatabase databaseWithPath:dbPath];
    
    if ([database open]) {
        NSLog(@"%@的个人数据库已打开!",[LHUserInfoManager shareManager].uname);
        
        //创建阅读收藏表
        [self createReadCollectTable];
    }
}

//关闭数据库
- (void)closeDB{
    if (database) {
        if ([database close]) {
            NSLog(@"%@的个人数据库已关闭!",[LHUserInfoManager shareManager].uname);
        }
    }
}

#pragma mark ---- 阅读收藏

//创建阅读收藏表
- (void)createReadCollectTable{
    BOOL createResult = [database executeUpdate:@"CREATE TABLE IF NOT EXISTS readCollect(contentid TEXT PRIMARY KEY,uname TEXT,title TEXT,content TEXT,coverimg TEXT,html TEXT)"];
    if (createResult) {
        NSLog(@"阅读收藏表正常存在!");
    }
}

//添加阅读收藏
- (BOOL)InsertReadCollectWithReadListModel:(LHReadListModel *)model HtmlStr:(NSString *)htmlStr{
    BOOL insertResult = [database executeUpdate:@"INSERT INTO readCollect(contentid,uname,title,content,coverimg,html) VALUES(?,?,?,?,?,?)",model.ID,model.name,model.title,model.content,model.coverimg,htmlStr];
    if (insertResult) {
        NSLog(@"添加阅读收藏成功!");
        return YES;
    }
    return NO;
}

//删除阅读收藏
- (BOOL)DeleteReadCollectWithContentID:(NSString *)contentID{
    BOOL deleteResult = [database executeUpdate:@"DELETE FROM readCollect WHERE contentid = ?",contentID];
    if (deleteResult) {
        NSLog(@"取消阅读收藏成功!");
        return YES;
    }
    return NO;
}

//获取收藏的所有阅读ID
- (NSArray *)selectAllReadCollectContentID{
    FMResultSet *resultSet = [database executeQuery:@"SELECT contentid FROM readCollect"];
    NSMutableArray *allContentIDArr = [NSMutableArray array];
    while ([resultSet next]) {
        NSString *contentID = [resultSet stringForColumnIndex:0];
        [allContentIDArr addObject:contentID];
    }
    return allContentIDArr;
}

//获取收藏的所有阅读内容
- (NSArray *)selectAllReadCollect{
    FMResultSet *resultSet = [database executeQuery:@"SELECT *FROM readCollect"];
    NSMutableArray *allReadCollectArr = [NSMutableArray array];
    while ([resultSet next]) {
        LHReadCollectModel *model = [[LHReadCollectModel alloc]init];
        model.ID = [resultSet stringForColumn:@"contentid"];
        model.content = [resultSet stringForColumn:@"content"];
        model.name = [resultSet stringForColumn:@"uname"];
        model.title = [resultSet stringForColumn:@"title"];
        model.coverimg = [resultSet stringForColumn:@"coverimg"];
        model.html = [resultSet stringForColumn:@"html"];
        [allReadCollectArr addObject:model];
    }
    return allReadCollectArr;
}

@end
